var WxParse = require('../../../wxParse/wxParse.js');
// const AV = require('../../../utils/av-weapp.js')

const util = require('../../../utils/util.js')
const app = getApp();

Page({
	data: {
		goods: {},
		current: 0,
		galleryHeight: getApp().screenWidth,
    goods_banner:[],
    gid:''
	},
	onLoad: function(options) {
    var goodsId = options.goodsId;
		this.getGoodsById(goodsId);
	},
	getGoodsById: function(goodsId) {
		var that = this
    let param = { gid: goodsId};
    util.commonRequest('index/Goods/detail','post',param,function(res){
      if(res.data.code == 406){
        wx.navigateBack({
          data:1
        })
      } else if(res.data.code == 200){
        that.setData({
          goods_banner: res.data.data.goods_banner,
          goods: res.data.data.goods,
          gid:res.data.data.goods.id
        })
        var article = res.data.data.goods.detail;
        WxParse.wxParse('article', 'html', article, that, 5);
      } else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    }) 
   },
   addCart: function() {
    //  addCart
     let that = this;
     let param = {
       user_id: app.globalData.userInfo.id,
       sku_str:'1:2',//后续完善商品规格这块
       gid: that.data.gid,
       num:1
     };
     util.commonRequest('index/Cart/addCart','post',param,function(res){
       
       if (res.data.code == 200){
         wx.showToast({
           title: res.data.msg,
         })
       } else{
         wx.showToast({
           title: res.data.msg,
           icon:'none'
         })
       }
     })
	 },
	previewImage: function (e) {
    let imag = e.currentTarget.dataset.img;
    let bannerList = this.data.goods_banner;
    let tempList = [];
    for(let i = 0; i < bannerList.length; i++){
      tempList.push(bannerList[i]['image']);
    }
    wx.previewImage({
      current: imag, 
      urls: tempList 
    })
	},
	showCart: function () {
		wx.switchTab({
			url: '../../cart/cart'
		});
	}
});